package com.brsya.videoprogressbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Created by Brsya
 * CreateDate: 2022/5/24  10:57
 * Description:
 * 视频播放进度条
 */
public class VideoProgressBar extends View {

    /**
     * 进度条背景色
     */
    private int backgroundColor;
    /**
     * 主进度条颜色
     */
    private int progressColor;
    /**
     * 副进度条颜色
     */
    private int progressSecondaryColor;
    /**
     * 进度条高度
     */
    private int progressHeight;
    /**
     * 滑块高度
     */
    private int blockHeight;
    /**
     * 滑块宽度
     */
    private int blockWidth;

    /**
     * 滑块
     */
    private RectF blockRect;
    /**
     * 主进度条
     */
    private RectF progressRect;
    /**
     * 副进度条
     */
    private RectF progressSecondaryRect;
    /**
     * 进度条背景
     */
    private RectF progressBackgroundRect;

    /**
     * View 宽度
     */
    private int width;
    /**
     * View 高度
     */
    private int height;

    /**
     * 最大value
     */
    private long maxProgressValue;

    /**
     * 当前进度
     */
    private long progressValue;

    /**
     * 副进度条当前进度
     */
    private long progressSecondaryValue;

    /**
     * 画笔
     */
    private Paint paint;

    /**
     * 手势相对于自身 X坐标
     */
    private float touchX;

    /**
     * 是否正在手势操作
     */
    private boolean isTouch = false;

    private OnProgressChangeListener onProgressChangeListener;


    public VideoProgressBar(Context context) {
        super(context);
        backgroundColor = 0x22FFFFFF;
        progressColor = 0xFFFFFFFF;
        progressSecondaryColor = 0x44FFFFFF;
        progressHeight = dpToPx(4);
        blockHeight = dpToPx(10);
        blockWidth = dpToPx(4);
        init();
    }

    public VideoProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
        init();
    }

    public VideoProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
        init();
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.video);
        backgroundColor = ta.getColor(R.styleable.video_backgroundColor, 0x22FFFFFF);
        progressColor = ta.getColor(R.styleable.video_progressColor, 0xFFFFFFFF);
        progressSecondaryColor = ta.getColor(R.styleable.video_progressSecondaryColor, 0x44FFFFFF);
        progressHeight = ta.getDimensionPixelSize(R.styleable.video_progressHeight, dpToPx(4));
        blockHeight = ta.getDimensionPixelSize(R.styleable.video_blockHeight, dpToPx(10));
        blockWidth = ta.getDimensionPixelSize(R.styleable.video_blockWidth, dpToPx(6));
    }

    private void init() {
        blockRect = new RectF();
        progressRect = new RectF();
        progressSecondaryRect = new RectF();
        progressBackgroundRect = new RectF();
        maxProgressValue = 100;
        progressValue = 0;
        progressSecondaryValue = 0;

        paint = new Paint();
        paint.setAntiAlias(true);
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = right - left;
        height = bottom - top;

        blockRect.top = (int) ((bottom - top - blockHeight) / 2f);
        blockRect.bottom = blockRect.top + blockHeight;

        progressBackgroundRect.left = 0;
        progressBackgroundRect.right = right - left;
        progressBackgroundRect.top = (int) ((bottom - top - progressHeight) / 2f);
        progressBackgroundRect.bottom = progressBackgroundRect.top + progressHeight;

        progressRect.left = 0;
        progressRect.top = (int) ((bottom - top - progressHeight) / 2f);
        progressRect.bottom = progressBackgroundRect.top + progressHeight;

        progressSecondaryRect.left = 0;
        progressSecondaryRect.top = (int) ((bottom - top - progressHeight) / 2f);
        progressSecondaryRect.bottom = progressBackgroundRect.top + progressHeight;

        calcRect();
    }

    /**
     * 重新计算位置信息
     */
    private void calcRect() {

        if (width <= 0) {
            return;
        }
        if (maxProgressValue == 0) {
            return;
        }
        progressRect.right = (int) (width * progressValue / maxProgressValue);
        progressSecondaryRect.right = (int) (width * progressSecondaryValue / maxProgressValue);

        blockRect.left = (int) (progressRect.right - (blockWidth / 2f));
        blockRect.right = blockRect.left + blockWidth;

        if (blockRect.left < 0) {
            blockRect.left = 0;
            blockRect.right = blockWidth;
        } else if (blockRect.right > width) {
            blockRect.right = width;
            blockRect.left = blockRect.right - blockWidth;
        }

        invalidate();
    }

    public int getBackgroundColor() {
        return backgroundColor;
    }

    @Override
    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public int getProgressColor() {
        return progressColor;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
    }

    public int getProgressSecondaryColor() {
        return progressSecondaryColor;
    }

    public void setProgressSecondaryColor(int progressSecondaryColor) {
        this.progressSecondaryColor = progressSecondaryColor;
    }

    public int getProgressHeight() {
        return progressHeight;
    }

    public void setProgressHeight(int progressHeight) {
        this.progressHeight = progressHeight;
    }

    public int getBlockHeight() {
        return blockHeight;
    }

    public void setBlockHeight(int blockHeight) {
        this.blockHeight = blockHeight;
    }

    public int getBlockWidth() {
        return blockWidth;
    }

    public void setBlockWidth(int blockWidth) {
        this.blockWidth = blockWidth;
    }

    public void setMaxProgressValue(long maxProgressValue) {
        this.maxProgressValue = maxProgressValue;
        calcRect();
    }

    public void setProgressValue(long progressValue) {
        if (isTouch) {
            // 正在手势操作,不更新主进度
            return;
        }
        this.progressValue = progressValue;
        calcRect();
    }

    public void setProgressSecondaryValue(long progressSecondaryValue) {
        this.progressSecondaryValue = progressSecondaryValue;
        calcRect();
    }

    public long getMaxProgressValue() {
        return maxProgressValue;
    }

    public long getProgressValue() {
        return progressValue;
    }

    public long getProgressSecondaryValue() {
        return progressSecondaryValue;
    }

    public void setOnProgressChangeListener(OnProgressChangeListener onProgressChangeListener) {
        this.onProgressChangeListener = onProgressChangeListener;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制进度条背景
        paint.setColor(backgroundColor);
        canvas.drawRoundRect(progressBackgroundRect, progressHeight / 2f, progressHeight / 2f, paint);

        // 绘制主进度条
        if (progressValue > 0) {
            paint.setColor(progressColor);
            canvas.drawRoundRect(progressRect, progressHeight / 2f, progressHeight / 2f, paint);
        }

        // 绘制主进度条
        if (progressSecondaryValue > 0) {
            paint.setColor(progressSecondaryColor);
            canvas.drawRoundRect(progressSecondaryRect, progressHeight / 2f, progressHeight / 2f, paint);
        }

        // 绘制滑块
        paint.setColor(progressColor);
        canvas.drawRoundRect(blockRect, 6, 6, paint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float touchXMove = event.getX();
        //                float rawX = event.getRawX();
        //                float rawY = event.getRawY();
        //                float Y = event.getY();

        //                Log.e("brsya_okhttp_:", "onTouchEvent: rawX: " + rawX + " rawY: " + rawY + " X: " + touchXMove + " Y: " + Y );

        /*
         * 计算点击位置代表的进度
         */
        long value = (long) ((long) touchXMove * maxProgressValue / width);
        if (value < 0) {
            // 最小为0
            value = 0;
        } else if (value > maxProgressValue) {
            // 最大为 maxProgressValue
            value = maxProgressValue;
        }
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                touchX = touchXMove;
                // 正在操作
                isTouch = true;
                break;
            case MotionEvent.ACTION_MOVE:
                progressValue = value;
                calcRect();
                if (onProgressChangeListener != null) {
                    onProgressChangeListener.changeProgressHint(progressValue);
                }
                break;
            case MotionEvent.ACTION_UP:
                isTouch = false;
                progressValue = value;
                calcRect();
                if (onProgressChangeListener != null) {
                    onProgressChangeListener.changeProgress(progressValue);
                }
                break;
        }


        return true;
    }

    /**
     * dp转px
     *
     * @param dp dp值
     * @return 转换后的px
     */
    public int dpToPx(float dp) {
        return (int) (getContext().getResources().getDisplayMetrics().density * dp);
    }


    public interface OnProgressChangeListener {
        void changeProgress(long progress);

        void changeProgressHint(long progress);
    }

}
